{
 "cells": [
  {
   "metadata": {},
   "cell_type": "raw",
   "source": "# 6.调用本地模型",
   "id": "a58bfea6facaed43"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-25T07:59:41.959482Z",
     "start_time": "2025-10-25T07:59:37.448576Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#调用本地私有模型\n",
    "# 举例1：\n",
    "from langchain_community.chat_models import ChatOllama\n",
    "\n",
    "#from langchain_ollama import ChatOllama\n",
    "ollama_llm = ChatOllama(\n",
    "    model=\"deepseek-r1:1.5b\",\n",
    "    # base_url=\"http://your-ip:port\" # 自定义地址\n",
    ")\n",
    "\n",
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "messages = [\n",
    "    HumanMessage(content=\"你好，请介绍一下你自己\")\n",
    "]\n",
    "chat_model_response = ollama_llm.invoke(messages)\n",
    "print(chat_model_response.content)"
   ],
   "id": "80c6d52b4d47571c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "\n",
      "</think>\n",
      "\n",
      "您好！我是由中国的深度求索（DeepSeek）公司开发的智能助手DeepSeek-R1。有关模型和产品的详细内容请参考官方文档。\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-25T08:03:04.995594Z",
     "start_time": "2025-10-25T08:02:49.607870Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "# 生成对话形式的聊天信息格式\n",
    "chat_prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"你是一个有用的助手，可以将{input_language}翻译成{output_language}。\"),\n",
    "    (\"human\", \"{text}\"),\n",
    "])\n",
    "# 格式化变量输入\n",
    "messages = chat_prompt.format_messages(input_language=\"中文\",\n",
    "                                       output_language=\"英语\",\n",
    "                                       text=\"我爱编程\")\n",
    "# 实例化Ollama启动的模型\n",
    "ollama_llm = ChatOllama(model=\"deepseek-r1:1.5b\")\n",
    "# 执行推理\n",
    "result = ollama_llm.invoke(messages)\n",
    "print(result.content)"
   ],
   "id": "ed2a502c77d62896",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<think>\n",
      "好的，用户让我翻译“我爱编程”成英文。首先，我要确保翻译准确，同时保持自然流畅。\n",
      "\n",
      "“我”翻译成“I”比较合适，直接对应。“爱”这里可以译为“I love”，这样不仅简洁，还能传达出爱意。“编程”则需要找到合适的表达方式，比如“programming”或者更专业的术语，但这里可能不需要那么复杂，所以用“programming”就可以了。\n",
      "\n",
      "组合起来就是“I love programming”，听起来很自然，也符合中文的口语化风格。同时，这句也可以用来引语或用于聊天，显得亲切和真诚。\n",
      "\n",
      "再想一下，有没有其他翻译方式？比如用“lile with programming”或者更详细地描述编程的过程，但这样可能显得有点冗长，且不太适合日常使用。所以我觉得“I love programming”是一个简洁而合适的翻译。\n",
      "\n",
      "最后检查一下，确保没有语法错误或拼写错误，觉得没问题就完成了。\n",
      "</think>\n",
      "\n",
      "I love programming.\n"
     ]
    }
   ],
   "execution_count": 7
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
